package com.chockqiu.demo.dialogs

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.graphics.Point
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.animation.doOnEnd
import androidx.fragment.app.DialogFragment
import com.chockqiu.demo.activitys.R
import com.chockqiu.view.ElementFloatTogetherAnimation

class ElementFloatTogetherDialog : DialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        super.onCreateView(inflater, container, savedInstanceState)
        val v: View = inflater.inflate(R.layout.dialog_ele_float_view, container, false)
        initView(v)
        return v
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initView(v: View) {
        var count = 0
        val viewStart = v.findViewById<View>(R.id.viewStart)
        val viewEnd1 = v.findViewById<View>(R.id.viewEnd1)
        val viewEnd2 = v.findViewById<View>(R.id.viewEnd2)
        val viewEnd3 = v.findViewById<View>(R.id.viewEnd3)
        val viewEnd4 = v.findViewById<View>(R.id.viewEnd4)
        val viewEnd5 = v.findViewById<View>(R.id.viewEnd5)
        val viewEnd6 = v.findViewById<View>(R.id.viewEnd6)
        val viewEnd7 = v.findViewById<View>(R.id.viewEnd7)
        val mElement = v.findViewById<ElementFloatTogetherAnimation>(R.id.mElement)
        mElement.elementDrawable = resources.getDrawable(R.drawable.icon_stone)
        mElement.elementNum = 20
        mElement.setOnClickListener {
            var endView = viewEnd1
            if (count % 7 == 0) {
                endView = viewEnd1
            } else if (count % 7 == 1) {
                endView = viewEnd2
            } else if (count % 7 == 2) {
                endView = viewEnd3
            } else if (count % 7 == 3) {
                endView = viewEnd4
            } else if (count % 7 == 4) {
                endView = viewEnd5
            } else if (count % 7 == 5) {
                endView = viewEnd6
            } else if (count % 7 == 6) {
                endView = viewEnd7
            }
            count++
            val p0 = Point().apply {
                x = (viewStart.left + viewStart.right) / 2
                y = (viewStart.top + viewStart.bottom) / 2
            }
            val p1 = Point().apply {
                x = (endView.left + endView.right) / 2
                y = (endView.top + endView.bottom) / 2
            }
            it.isClickable = false
            mElement.elementDrawableSize = 20
            mElement.startAnimation(p0, p1) {
                it.isClickable = true
                startSlightZoomAnimation(endView) {}
            }
        }
    }

    /**
     * 轻微快速缩放动画
     *
     * @param view 目标View
     * @param block 动画结束回调
     */
    fun startSlightZoomAnimation(view: View, block: () -> Unit) {
        val sc1 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.2f, 1f, 1.2f, 1f)
        val sc2 = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.2f, 1f, 1.2f, 1f)
        AnimatorSet().apply {
            playTogether(sc1, sc2)
            duration = 500
            doOnEnd { block.invoke() }
            start()
        }
    }
}